package com.qf.dao.impl;

import com.qf.dao.AddressDao;
import com.qf.pojo.TbAddress;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;


/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/8/15 19:02
 */
public class AddressDaoImpl implements AddressDao {
    private QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
    @Override
    public void insertAddress(TbAddress tbAddress) {
        try {
            String sql="insert into tb_address(detail,name,phone,uid,level) values (?,?,?,?,?)";
            Object[] params={tbAddress.getDetail(),tbAddress.getName(),tbAddress.getPhone(),tbAddress.getUid(),tbAddress.getLevel()};
            queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<TbAddress> selectByUid(Long uid) {
        String sql="select * from tb_address where uid=?";
        try {
            return queryRunner.query(sql, new BeanListHandler<>(TbAddress.class),uid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public TbAddress selectAddress(long aid) {
        String sql="select * from tb_address where id=?";
        try {
            return queryRunner.query(sql, new BeanHandler<>(TbAddress.class),aid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
